package org.leslie.controller;

import org.leslie.pojo.Result;
import org.leslie.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        // 把文件的内容存储到磁盘
        String originalName = file.getOriginalFilename();
        // UUID to avoid rewrite file name
        String filename = null;
        if (originalName != null) {
            filename = UUID.randomUUID().toString() + originalName.substring(originalName.lastIndexOf("."));
        }
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
